package com.livelab.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import com.livelab.util.Constant;

/**
 * Utility class that provides basic I/O related functionality
 *
 * @author Dakota
 */
public class Utility {

    /**
     * Make a file directory
     *
     * @param path -- File path of directory
     * @return status of creating a directory
     */
    public static boolean makeDir(String path) {
        File newDir = new File(path);
        return newDir.mkdirs();
    }

    /**
     * Read a file
     * 
     * @param path -- Path to file
     * @return Contents of file
     */
    public static String readFile(String path) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            StringBuilder str = new StringBuilder();
            String line;
            
            //read the whole file
            while ((line = br.readLine()) != null) {
                str.append(line);
                str.append(Constant.DELIMETER);
            }

            return str.toString();
        } catch (IOException e) {
            System.out.println("Error");
        }
        return "Error";
    }

    /**
     * Compare two strings together for equality
     * 
     * @param s1 -- student's code-generated output
     * @param s2 -- Output key
     * @return Result of comparison
     */
    public static boolean grade(String s1, String s2) {
        for (String s : s2.split("#")) {
            if (!s1.contains(s)) {
                return false;
            }
        }

        return true;
    }
}
